package fr.bruno.puzzlefrance;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		View view = (View) findViewById(R.id.RelativeLayout);
		view.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				String msg = String.format("Coordonne : (%.0f, %.0f)",
						event.getRawX(), event.getRawY());
				Log.i("HHH", msg);

				return false;
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Log.i("HHH", "MENU : " + item.getItemId());

		switch (item.getItemId()) {

		case R.id.menu_new:
			Log.i("HHH", "NOUVELLE PARTIE");
			this.recreate();
			return true;

		case R.id.menu_option:
			Log.i("HHH", "OPTION");

			View view = findViewById(R.id.RelativeLayout);
			getAllChildren(view);

			return true;

		case R.id.menu_help:
			getResources().getLayout(R.layout.activity_main);
			return true;

		default:
			return super.onContextItemSelected(item);
		}

	}

	// @Override
	// public boolean onContextItemSelected(MenuItem item) {
	//
	// Log.i("HHH", "MENU : " + item.getItemId());
	//
	// switch (item.getItemId()) {
	//
	// case R.id.menu_new:
	// Log.i("HHH", "NOUVELLE PARTIE");
	// this.recreate();
	// return true;
	//
	// case R.id.menu_option:
	// Log.i("HHH", "OPTION");
	//
	// View view = findViewById(R.id.AbsoluteLayout1);
	// getAllChildren(view);
	//
	// return true;
	//
	// case R.id.menu_help:
	// getResources().getLayout(R.layout.activity_main);
	// return true;
	//
	// default:
	// return super.onContextItemSelected(item);
	// }
	// }

	private void getAllChildren(View v) {

		ViewGroup vg = (ViewGroup) v;

		for (int i = 0; i < vg.getChildCount(); ++i) {

			View child = vg.getChildAt(i);

			String s = getResources().getResourceName(child.getId());

			Log.i("HHH", s + " : " + child.getX() + "," + child.getY()
					+ " ===> " + (child.getX() + 26) + ","
					+ (child.getY() + 109));

			// getAllChildren(child);
		}
	}
}
